package search.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import model.Program;
import search.Parser;

public class ParserImpl implements Runnable,Parser {

	private Program program;
	private List<Program> programs;
	private String html;
	
	public void parse2Programs(List<Program> programs,String html) {
		this.programs = programs;
		this.html = html;
		Thread pThread = new Thread(this);
		// -- 0
		pThread.setName(html);
		// -- 1
		pThread.start();
	}
	public void run() {
		
		// -- 0
	//	System.out.println("正在运行的线程: "+ Thread.currentThread().getName());
		// -- 1
		
		parse(this.html);
		//testcode -- 0
	//	System.out.println(program);
		//testcode -- 1
	}
	public void parse(String html) {
		Program p = new Program();
		try {
			Document document = Jsoup.connect(html).timeout(999999999).get();
			Element program = document.getElementById("page-title");	//program-name
			Elements group_left = document.getElementsByClass("group-left");
			Elements linkContents  = group_left.get(0).select("a");// list of homePage and email
			Element homePage = linkContents.get(0);//homePage
			Element email = linkContents.get(linkContents.size()-1);//email
			Elements groupLeftChildNodes =  group_left.get(0).select(".field-item");
			List<Element> location  = new ArrayList<Element>(); //location 由两部分组成
			location.add(groupLeftChildNodes.get(1));
			location.add(groupLeftChildNodes.get(3));
			Element phone = groupLeftChildNodes.get(groupLeftChildNodes.size()-1);//phone
			Elements group_right = document.getElementsByClass("group-right");
			Element school = group_right.get(0).select("a").get(0);
			
			Elements tbodys = document.getElementsByTag("tbody");
			
			String combineDegree = new String();
			String combineDDLWithAid = new String();
			String combineDDLWithoutAid = new String();
			if(!tbodys.isEmpty()) {		//在读取的过程中发现存在表格不存在的情况
				Elements trs = tbodys.get(0).select("tr");
				
				//联合degree String -- 0

				for(int i=0;i<trs.size();i++)
				{
					combineDegree += trs.get(i).select("td").get(0).text().trim();
					if(i!=trs.size()-1) {
						combineDegree += "\t";
					}		
				}
				if(combineDegree.trim().isEmpty()) {
					combineDegree = "NULL";
				}
				//联合degree String -- 1
				//联合ddlWithAid String -- 0

				for(int i=0;i<trs.size();i++)
				{
					combineDDLWithAid += trs.get(i).select("td").get(1).text().trim();
					if(i!=trs.size()-1) {
						combineDDLWithAid += "\t";
					}		
				}
				if(combineDDLWithAid.trim().isEmpty()) {
					combineDDLWithAid = "NULL";
				}
				//联合ddlWithAid String -- 1
				//联合ddlWithoutAid String -- 0

				for(int i=0;i<trs.size();i++)
				{
					combineDDLWithoutAid += trs.get(i).select("td").get(2).text().trim();
					if(i!=trs.size()-1) {
						combineDDLWithoutAid += "\t";
					}		
				}
				if(combineDDLWithoutAid.trim().isEmpty()) {
					combineDDLWithoutAid = "NULL";
				}
				//联合ddlWithoutAid String -- 1
			}
			
			//视情况将提取内容写入信息体 Program p 中
		
			// 这里先不设置32位id, 在写入文件的时候设置32位id
			p.setCountry("United States");				//country
			p.setUniversity("Stanford University");	//university
			if(tbodys.isEmpty()) {
				p.setDegree("NULL");
				p.setDeadlineWithAid("NULL");
				p.setDeadlineWithoutAid("NULL");
			}else {
				p.setDegree(combineDegree);	//在之前已经判断过是否为空
				p.setDeadlineWithAid(combineDDLWithAid);	//在之前已经判断过是否为空
				p.setDeadlineWithoutAid(combineDDLWithoutAid);	//在之前已经判断过是否为空
			}
			if(program.text().trim().isEmpty()) {	//program-name
				p.setProgramName("NULL");
			}else {
				p.setProgramName(program.text().trim());
			}
			if(homePage.text().trim().isEmpty()) { // homepage
				p.setHomepage("NULL");
			}else {
				p.setHomepage(homePage.text().trim());
			}
			if(email.text().trim().isEmpty()||linkContents.size()<=1) { // email
				p.setEmail("NULL");
			}else {
				p.setEmail(email.text().trim());
			}
			if(location.get(0).text().trim().isEmpty()&&location.get(1).text().trim().isEmpty()) {//location
				p.setLocation("NULL");
			}else {
				p.setLocation(location.get(0).text().trim()+" "+location.get(1).text().trim());
			}
			if(phone.text().trim().isEmpty()||groupLeftChildNodes.size()<=5) {  //phone````````````````````````````````````````````````````````````````````
				p.setPhoneNumber("NULL");
			}else {
				p.setPhoneNumber(phone.text());
			}
			if(school.text().trim().isEmpty()) {  //school
				p.setSchool("NULL");
			}else {
				p.setSchool(school.text());
			}
			
			synchronized (this.programs) {
						programs.add(p);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
//	private Program setProgramNULL(Program p) {
//		p.setCountry("NULL");
//		p.setDeadlineWithAid("NULL");
//		p.setDeadlineWithoutAid("NULL");
//		p.setDegree("NULL");
//		p.setEmail("NULL");
//		p.setHomepage("NULL");
//		p.setHomepage("NULL");
//		p.setId("NULL");
//		p.setLocation("NULL");
//		p.setPhoneNumber("NULL");
//		p.setProgramName("NULL");
//		p.setSchool("NULL");
//		p.setUniversity("NULL");
//		return p;
//	}
	static class RandomGenerator{
		static String getRandomNumber() {
			UUID uuid = UUID.randomUUID();
			String id = uuid.toString().replace("-","");
			id = id.toUpperCase();		//转成大写装逼点
			return id;
		}
	}
}
